<?php
if (!defined('INPOL')) return2();
try
{
    #MUST HAVE VALUES
    $ins_comp=getVar('ins_comp', 'int');
    $pol_nr=getVar('pol_nr');
    $pol_add_nr=getVar('pol_add_nr');
    $pol_attr=getVar('pol_attr', 'bool');
    $pol_canc=getVar('pol_canc','bool');
    $ins_type=getVar('ins_type');

    #POSIBLE NULL VALUES
    $status=getVar('status');
    $status=SQLNull($status);
    $issue_date=getVar('issue_date','date',true);
    $issue_date=SQLNull($issue_date);
    $persons_count=getVar('persons_count','int',true);
    $persons_count=SQLNull($persons_count);
    
    $prev_pol=getVar('prev_pol');
    $taker=getVar('taker', 'int');
    $owner=getVar('owner', 'int', true);
    //$obj_type1=getVar('obj_type1','int'); //not used?
    //$obj_type2=getVar('obj_type2','int'); //not used?
    $object1=getVar('object1','int', true);
    $object2=getVar('object2','int', true);
    $cta_premium=getVar('cta_premium', 'float', true);
    $ng_premium=getVar('ng_premium','float', true);
    
    $octa_code=getVar('TL_codes', 'int', true);
    $octa_example=getVar('octa_premium','int', true);
    $octa_discounts=getVar('octa_discounts', 'int', true);
    $riska_class=getVar('riska_class');
    $val_ter=getVar('val_ter','int', true);
    
    
    $date_from=getVar('date_from','date');
    $date_from_time=getVar('date_from_time','time');
    $date_to=getVar('date_to','date');
    $payment_count=getVar('payment_count','int');
    $payment_type=getVar('payment_type', 'int', true);
    $currency=getVar('currency', 'int');
    
    $ins_sum=getVar('ins_sum','float', true);
    $ins_prem=getVar('ins_prem','float');
    $val_ter=getVar('val_ter', 'int', true);
    $policy_subscriber=getVar('policy_subscriber','int', true);
    $commision_perc=getVar('commision_perc', 'float');
    $broker_perc=getVar('broker_perc', 'float', true);
    $broker=getVar('broker', 'int', true);
    $discount_perc=getVar('discount_perc','float', true);
    $comment=getVar('comment');
    $employee=getVar('employee','int');

    $sql->query("INSERT INTO policies 
    (nr, add_nr, ins_comp_id, ins_type_id, state_id, prev_nr, taker_id, owner_id, issue_date,
     from_date, from_time, to_date, payment_count, payment_type, currency_id, ins_sum, ins_prem,
      persons_count, octa_code_id, octa_example_id, octa_discount_id, octa_risks, 
      validity_territory_id, cta_premium, ng_premium, represent_id, comment, 
      client_attraction, comm_rate, add_disc_rate, broker_rate, broker_id, employee_id) 
    VALUES 
    ('$pol_nr', '$pol_add_nr', '$ins_comp', '$ins_type', $status, '$prev_pol', '$taker', '$owner', $issue_date,
    '$date_from', '$date_from_time', '$date_to', '$payment_count', '$payment_type', '$currency', '$ins_sum', '$ins_prem',
    $persons_count, '$octa_code', '$octa_example', '$octa_discounts', '$riska_class',
    '$val_ter', '$cta_premium', '$ng_premium', '$policy_subscriber', '$comment',
    '$pol_attr', '$commision_perc', '$discount_perc', '$broker_perc', '$broker', '$employee')");
    if($sql->row_count)
        msg($Lang['success'], $Lang['success_add']);
    else
        throw new Exception($Lang['failed_to_add']);
    $pId=$sql->getId();
    if($object1!=null && $object1!='' && $object1!=0 && $object1!='0')
    {
        $sql->query("INSERT INTO pol_obj VALUES('$pId', '$object1');");
        if($sql->row_count)
            msg($Lang['success'], sprintf($Lang['object_assigned_policy'], '1'));
        else
            throw new Exception(sprintf($Lang['failed_to_assign_object'],'1'));
    }
    else
    {
        msg($Lang['warning'], sprintf($Lang['policy_without_object'], '1'),'warning');
    }
    if($object2!=null && $object2!='' && $object2!=0 && $object2!='0')
    {
        $sql->query("INSERT INTO pol_obj VALUES('$pId', '$object2');");
        if($sql->row_count)
            msg($Lang['success'], sprintf($Lang['object_assigned_policy'], '2'));
        else
            throw new Exception(sprintf($Lang['failed_to_assign_object'],'2'));
    }
    else
    {
        msg($Lang['warning'], sprintf($Lang['policy_without_object'], '2'),'warning');
    }
    echo '<button type="button" class="yt_button" onclick="Redirect(\'?p='.$page.'&a=view&id='.$pId.'\');" >'.$Lang['view_policy'].'</button>';
}
catch(Exception $e)
{
    msg($Lang['error'], $e->getMessage(), 'error');
}
?>